package com.baoteng.scan;

import android.app.Activity;
import android.content.res.AssetFileDescriptor;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.RelativeLayout;

import com.sunlighttech.ihotel.qr.InitElement;
import com.sunlighttech.ihotel.qr.camera.CameraManager;

/**
 * Author: loren
 * Date: 2019/9/17
 */
public class ScannerImpl implements IScan {

    private static ScannerImpl instance;

    public static ScannerImpl getInstance() {
        if (instance == null) {
            synchronized (ScannerImpl.class) {
                if (instance == null) {
                    instance = new ScannerImpl();
                }
            }
        }
        return instance;
    }

    private InitElement element;

    @Override
    public void init(Activity con, RelativeLayout mContainer, RelativeLayout mCropLayout, AssetFileDescriptor file, SurfaceView surfaceView) {
        element = new InitElement(con, mContainer, mCropLayout, file);
        element.initSurfaceHolder(surfaceView);
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        element.surfaceDestroyed(holder);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        element.surfaceCreated(holder);
    }

    @Override
    public void actDestory() {
        element.actDestory();
    }

    @Override
    public void actPause() {
        element.actPause();
    }

    @Override
    public void switchFlashLight(boolean open) {
        try {
            CameraManager cameraManager = CameraManager.get();
            if (open) {
                cameraManager.openLight();
            } else {
                cameraManager.offLight();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
